package cn.sumpu.app.wlcity.biz.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.sumpu.app.wlcity.biz.ContentBiz;
import cn.sumpu.app.wlcity.dao.ContentDao;
import cn.sumpu.app.wlcity.domain.Content;
import cn.sumpu.app.wlcity.exception.AppBusinessException;
import cn.sumpu.app.wlcity.util.Common;
import cn.sumpu.app.wlcity.util.IdGenerator;

public class ContentBizImpl implements ContentBiz {

	private ContentDao contentDao;

	public void setContentDao(ContentDao contentDao) {
		this.contentDao = contentDao;
	}

	@Override
	public List<Content> getContentList(Integer page, String contentType,String radioApp)
			throws AppBusinessException {
		if (contentType == null)
			contentType = "01";
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("startId", (page - 1) * Common.PAGE_SIZE_CONTENT);
		map.put("pageSize", Common.PAGE_SIZE_CONTENT);
		map.put("contentType", contentType);
		map.put("app", radioApp);
		return contentDao.getContentList(map);
	}
	

	@Override
	public List<Content> getContentHotList(Integer page, String contentType,String radioApp)
			throws AppBusinessException {
		if (contentType == null)
			contentType = "01";
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("startId", (page - 1) * Common.PAGE_SIZE_HOT);
		map.put("pageSize", Common.PAGE_SIZE_HOT);
		map.put("contentType", contentType);
		map.put("app", radioApp);
		return contentDao.getContentList(map);
	}

	@Override
	public void insertContent(Content content) throws AppBusinessException {
		contentDao.upEachContentSort(content.getContentType());
		content.setIdentifier(IdGenerator.createIdentifier());
		content.setSort(1);
		contentDao.insertContent(content);
	}

	@Override
	public Content getContentById(String identifier)
			throws AppBusinessException {
		return contentDao.getContentById(identifier);
	}

	@Override
	public void updateContent(Content content) throws AppBusinessException {
		contentDao.updateContent(content);

	}

	// 上移
	@Override
	public void moveContentSortUp(Content content,String contentType) throws AppBusinessException {
		int nowSort = content.getSort();
		if (nowSort > 0) {
			// 上面的移下去
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("originalSort", nowSort - 1);
			map.put("nowSort", nowSort);
			map.put("contentType", contentType);
			contentDao.moveContentSort(map);
			// 当前移上去
			content.setSort(nowSort - 1);
			contentDao.updateContent(content);
		}

	}

	// 下移
	@Override
	public void moveContentSortDown(Content content,String contentType)
			throws AppBusinessException {
		int nowSort = content.getSort();
		Map<String, Object> m=new HashMap<String, Object>();
		m.put("contentType", contentType);
		// 下面的移上去
		if (nowSort < contentDao.getContentListCount(m)) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("originalSort", nowSort + 1);
			map.put("nowSort", nowSort);
			map.put("contentType", contentType);
			contentDao.moveContentSort(map);
			// 当前移下去
			content.setSort(nowSort + 1);
			contentDao.updateContent(content);
		}

	}

	@Override
	public void deleteContent(String identifier) throws AppBusinessException {
		contentDao.deleteContent(identifier);
	}

	@Override
	public int getContentListCount(String contentType,String radioApp)
			throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("contentType", contentType);
		map.put("app", radioApp);
		return contentDao.getContentListCount(map);
	}

	@Override
	public void downEachVoteSort(Integer sort, String contentType)
			throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("contentType", contentType);
		map.put("sort", sort);
		contentDao.downEachVoteSort(map);
	}

}
